<?php
require_once '../includes/auth.php';
check_role('user');

$searchTerm = clean_input($_GET['q'] ?? '');

// 多表联合搜索
$results = [];
if (!empty($searchTerm)) {
    $stmt = $pdo->prepare("
        (SELECT id, name_zh, 'ship' AS type FROM ships 
         WHERE name_zh LIKE :term OR name_en LIKE :term OR imo_number = :term)
        UNION
        (SELECT id, name_zh, 'port' AS type FROM ports 
         WHERE MATCH(name_zh, name_en) AGAINST(:term IN BOOLEAN MODE))
        UNION
        (SELECT id, name_zh, 'anchor' AS type FROM anchors 
         WHERE MATCH(name_zh, name_en) AGAINST(:term IN BOOLEAN MODE))
        ORDER BY name_zh LIMIT 50
    ");
    $stmt->execute([':term' => "%$searchTerm%"]);
    $results = $stmt->fetchAll();
}
?>